﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Scripting.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    using Unit = CommonUnit;
    using ConversionFactor = CommonConversionFactor;

    public static class CommonUnitExtension
    {
        /// <summary>
        /// 获得 单位列表
        /// </summary>
        /// <returns></returns>
        public static List<Unit> getUnitList(this CommonUnit unit, CoreLimsContext dbContext) => dbContext.Unit.ToList();

        /// <summary>
        /// 根据 单位代码 ，获得单位信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Unit getUnit(this CommonUnit unit, CoreLimsContext dbContext, string Code) =>
            dbContext.Unit.FirstOrDefault(m => m.Code == Code);

        /// <summary>
        /// 添加单位
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static string AddUnit(this CommonUnit unit, CoreLimsContext dbContext)
        {
            CommonUnit cunit = Common.Unit.getUnit(dbContext, unit.Code);
            if (cunit == null)
            {
                dbContext.Add(unit);
                dbContext.SaveChanges();
                return "单位添加成功！";
            }
            else
                return "该单位代码已经存在！";
        }

        /// <summary>
        /// 删除单位
        /// </summary>
        /// <param name="Code"></param>
        public static string DeleteUnit(this CommonUnit unit, CoreLimsContext dbContext, string Code)
        {
            CommonUnit cunit = Common.Unit.getUnit(dbContext, Code);
            int list = cunit.getConversionFactorList(dbContext, Code).Count;

            if (list == 0)
            {
                dbContext.Remove(cunit);
                dbContext.SaveChanges();
                return "删除成功！";
            }
            else
                return "该单位存在关联的转换因子！";
        }

        /// <summary>
        /// 获得自身的转换列表
        /// </summary>
        /// <param name = "SourceCode" ></ param >
        /// < returns ></ returns >
        public static List<ConversionFactor> getConversionFactorList(this CommonUnit unit, CoreLimsContext dbContext, string Code) =>
            Common.ConversionFactor.getFactorBySourceCode(dbContext, Code).ToList();
    }

    public static class ConversionFactorExtension
    {
        /// <summary>
        /// 根据 Id ，获得转换因子
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static ConversionFactor getFactor(this CommonConversionFactor ccf, CoreLimsContext dbContext, int Id) =>
            dbContext.ConversionFactor.FirstOrDefault(m => m.Id == Id);

        /// <summary>
        /// 根据 代码 和 目标代码 查询该转换
        /// </summary>
        /// <param name="SourceCode"></param>
        /// <param name="TargetCode"></param>
        /// <returns></returns>
        public static List<ConversionFactor> getConversionFactor(this CommonConversionFactor ccf, CoreLimsContext dbContext, string SourceCode, string TargetCode)
            => dbContext.ConversionFactor.Where(m => m.Code == SourceCode && m.TargetCode == TargetCode).ToList();

        /// <summary>
        /// 获得指定源单位代码的转换列表
        /// </summary>
        /// <param name="SourceCode"></param>
        /// <returns></returns>
        public static List<ConversionFactor> getFactorBySourceCode(this CommonConversionFactor ccf, CoreLimsContext dbContext, string SourceCode) =>
            dbContext.ConversionFactor.Where(m => m.Code == SourceCode).ToList();

        /// <summary>
        /// 获得目标单位的转换因子，如果没有返回 null
        /// </summary>
        /// <param name="TargetCode"></param>
        /// <returns></returns>
        public static decimal? getFactorByTargetCode(this CommonConversionFactor ccf, CoreLimsContext dbContext, string TargetCode)
        {
            //var clist = Common.Unit.getConversionFactor(dbContext);
            //var factor = clist.FirstOrDefault(m => m.TargetCode == TargetCode);

            //if (factor is null)
            //    return null;
            //else
            //    return factor.Factor;

            return null;
        }

        /// <summary>
        /// 添加转换因子
        /// </summary>
        /// <param name="factor"></param>
        /// <returns></returns>
        public static string AddConversionFactor(this CommonConversionFactor unit, CoreLimsContext dbContext, CommonConversionFactor factor)
        {
            List<ConversionFactor> ccf = Common.ConversionFactor.getConversionFactor(dbContext, factor.Code, factor.TargetCode);
            if (ccf.Count == 0)
            {
                dbContext.Add(factor);
                dbContext.SaveChanges();
                return "转换因子添加成功！";
            }
            else
                return "该转换已经存在！";
        }

        /// <summary>
        /// 删除转换因子
        /// </summary>
        /// <param name="Id"></param>
        public static string DeleteConversionFactor(this CommonConversionFactor unit, CoreLimsContext dbContext, string Id)
        {
            int id = 0;
            int.TryParse(Id, out id);
            CommonConversionFactor factor = Common.ConversionFactor.getFactor(dbContext, id);
            dbContext.Remove(factor);
            dbContext.SaveChanges();
            return "删除成功！";
        }
    }

}
